These are my ideas about the grammar of ggplot2. ggplot2 is based on the grammar of graphics. But it has its own grammar too — it is a particular implementation of the grammar of graphics — and a lot of really helpful decisions are part of its grammar (like having set defaults for aesthetic scales, and scale and plot labels make it easier to build plots quickly). If you are teaching ggplot2, you might think about teaching the grammar of ggplot2 first and then come back to the grammar of graphics — because there will be more context for seeing how an implemenation relates to the larger philosophy. There are a lot of great resources out there already teaching the grammar of graphics which are sources of inspiration for the guide.
Not into human language grammar? Just ignor that first column!
| Grammar Analogy | How? | What? | |
|---|---|---|---|
| 1. The Declarative Mood | ggplot(data = gapminder) + | Declaring the data | |
| 2. The Interogative Mood | aes(color = pop) + | Asking for representation of variables by aesthetics (color, size, x position, etc.) (also known as aesthetic mapping) | |
| - Modifiers I | labs(color = continent) + labs(title = “my title”) + |
modifying default aesthetic labels (and plot labels) | |
| - Modifiers II | coord_polar() + | modifying the default coordinate system (how the positional aesthetics appear - x and y) | |
| - Modifiers III | scale_color_viridis_d() + | modifying default aesthetic scales | |
| 3. Nouns | geom_point() + | geometric layers taking on the aesthetics representing variables | |
| 4.The Conditional Mood | geom_point( | Making local, geom-specific declarations rather than global declarations | |
| data = gapminder, | data is geom-specific |
||
| aes(size = population), | aesthetic representation is geom-specific |
||
| color = “blue” |
aesthetics not representing variables; unmapped aesthetics (i.e. The Imperative Mood) | ||
| ) + | |||
| 5. Interjections | annotate(geom = “point”, x = 10, y = 12, color = “blue”) |
Adding context with annotation layers | |
| 6. Punctuation | facet_wrap(~continent) | faceting breaks a plot into small plots (or “small multiples”) based on categorical variables | |
| 7. Greetings | theme_minimal() | themes changing plot look and feel | |
| 8. The Written Language | ggsave(file = “plot.png”, plot = g, height = “4in”, width = “6in”) |
save plots with different resolutions and file formats | |
| 9. Composition | library(patchwork) (g1 + g2) | g3 library(cowplot) plotgrid(…,) |
Composing plots into ensembles | |
| 10. Concision | last_plot(), switch out data with %+%, writing functions |
We’ve been intentionally verbose, but there are strategies for being concise |
Above, the geom topics are intentionally cursory. There are tons of geoms, and they can get distracting. But geoms are pretty fun - and there are a lot!
A Closer Look at Color
A Closer Look at Text
r viztoc:::build_and_use_gif(dir = “ggtext_files/figure-html/”, pattern = "" , href = “ggtext.html” )`
External Theme Packages
Network visualization {ggraph} and {tidygraph}
Animation with {gganimate}
ggplot2 is very flexible, so there often there are more than one way of getting things done — which also means there is more than one way to introduce ggplot2. If you aren’t on board with this guide (and even if you are) you may want check out other resouces. The more definitive and comprehensive guide to ggplot2 is the 3rd edition of ggplot2: elegant graphics for data analysis. Some other workshop materials are Alison Hill’s Take a Sad Plot and Make it Better, Garrick Aden-Buie’s A Gentle Guide to the Grammar of Graphics with ggplot2, Will Chase’s R you ready to make charts?, Malcom Barrett’s Designing ggplots making clear figures that communicate, and Thomas Lin Pederson’s Drawing anything with ggplot2. The ggplot2 cheatsheet is also excellent.